<?php
/**
 * Enter description : Điều khiển đăng ký va login 
 * @author admin888 <doan.hut@gmail.com >
 *
 */
class  Model_Login extends Zend_Db_Table
{
	protected  $dangnhap = "thanhvien";
	protected  $_tv = "thanhvien";
	protected  $_db ;
	public function __construct()
	{
	  $this->_db =  Zend_Registry::get('connectDb');
	}
	/**
	 * admin 888 <paven@gmail.com>
	 * Enter description : insert 
	 * @param unknown_type $ten
	 * @param unknown_type $uname
	 * @param unknown_type $pass
	 * @param unknown_type $email
	 */ 
	public function addUsers($ten,$uname,$pass,$email)
	{
		$dangnhap = "thanhvien";
		//$date = date('Y-m-d');
		$date = date('m-d-Y H:i:s');
		$khoachinh = "uid";
		$data = array(
			'name' 	   =>$ten,
			'uname'    =>$uname,
			'pass'     => $pass,
			'leve' 	   => 3,
			'kichhoat' => 0,
			'ngay_tao' => $date,
			'ngay_sua' => $date,
			'email'    => $email
		);
		$query =  $this->_db->insert($dangnhap, $data);
		$id = $this->_db->lastInsertId($dangnhap,$khoachinh);
	return $id;
	}
/**
 * admin888 <paven@gmail.com>
 * Enter description :lay 1 gia cua id vua insert
 * @param unknown_type $them
 */
	public function getName($them)
	{
		$dangnhap = "thanhvien";
		$sql = $this->_db->select()
					->from($dangnhap)
					->where('uid = ?',$them);
		$query = $this->_db->fetchRow($sql);
	return $query;				
	}
/**
 * admin888 <doan.hut@gmail.com>
 * Enter description :check login va khởi tạo sisson
 * @param unknown_type $tendn
 * @param unknown_type $matkhau
 * @param unknown_type $ghinho
 */
	public function checklogin($tendn,$matkhau)
	{
		$dangnhap = "thanhvien";
		$db = Zend_Registry::get('connectDb');// dang ky connec
		$auth = Zend_Auth::getInstance();//2. Khoi tao Zend Autho
		$authAdapter = new Zend_Auth_Adapter_DbTable($db);
		$authAdapter->setTableName($dangnhap)
					->setIdentityColumn('uname')
					->setCredentialColumn('pass');
		$authAdapter->setIdentity($tendn)
					->setCredential($matkhau);
		$select = $authAdapter->getDbSelect();	
				       $select->where('kichhoat = 1');
		$result = $authAdapter->authenticate($auth);
		// neu user va pass nguoi dung nhap vao la dung thi luu vao doi tuong auth
		if ($result->isValid()){
			$data =  $authAdapter->getResultRowObject(null, array('pass'));
			$auth->getStorage()->write($data);
			
			return  true; 	
		}else{
			return  false; 
		} 
				       										
				
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : lay du lieu trong bang cau_hinh_user co leve = 1
	 * @return Ambigous <multitype:, mixed>
	 */
	public function getSeting()
	{
		$cau_hinh  = "cau_hinh_user";
   		$dangnhap = "thanhvien"; 
		// lấy id của lever = 1 administrator
		$sql = $this->_db->select()
						 ->from($dangnhap,'uid')
						 ->where('leve = ?',1);
		$uid = $this->_db->fetchOne($sql);
		//lay du lieu tu bang cau hinh theo id administrator
		$truyvan = $this->_db->select()
							 ->from($cau_hinh)
							 ->where('uid  = ? ',$uid);
		$dulieu = $this->_db->fetchRow($truyvan);
	return $dulieu;				 
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description Check trung usser dang nhập thành viên 
	 * @param unknown_type $data
	 */
	public function checktrunguser($data)
	{
		$sql=  $this->_db->select()
					   ->from($this->_tv,array("COUNT('uid') AS tong"))
					   ->where($this->_tv.".uname ='".$data['name']."'");
			$query = $this->_db->fetchOne($sql);
			
			return $query;				   	
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : update thoi gian login của thành viên 
	 * @param unknown_type $uid
	 */
	public function  timelogin($uid)
	{
		$date = date('m-d-Y H:i:s');
		$arr = array(
			'ngay_login' =>$date
		);
		$where = "uid =".$uid;
	return  $this->_db->update($this->_tv,$arr,$where);	
	}

}

?>